import React, {
	Component
} from 'react';
import {
	inject,
	observer
} from 'mobx-react';

@inject('forgetPasswordStore')
@observer
export default class Timer extends Component {
	constructor(props) {
		super(props);
		this.timer = null;
	}

	componentDidMount() {
		const{countDown}=this.props.forgetPasswordStore;
		this.timer = setInterval(countDown, 1000)
	}

	componentWillUnmount() {
		if (this.timer) clearInterval(this.timer);
	}

	componentDidUpdate(prevProps, prevState) {
		const {
			getCount
		} = this.props;

		const{count}=this.props.forgetPasswordStore;
		getCount(count);

		if (count === 0) {
			clearInterval(this.timer);
		}
	}

	render() {
		const{count}=this.props.forgetPasswordStore;
		return (
			<span>{`${count}`}</span>
		);
	}
}